//
//  CAKeyframeAnimation+GBAnimation.m
//  GuiJuWo
//
//  Created by chenguibang on 16/4/5.
//  Copyright © 2016年 chenguibang. All rights reserved.
//

#import "CAKeyframeAnimation+GBAnimation.h"
static int numberOfShakes = 4;//震动次数
static float durationOfShake = 0.2f;//震动时间
static float vigourOfShake = 0.01f;//震动幅度
@implementation CAKeyframeAnimation(GBAnimation)
+ (CAKeyframeAnimation *)shakeAnimation:(CGRect)frame
{
    CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    CGMutablePathRef shakePath = CGPathCreateMutable();
    CGPathMoveToPoint(shakePath, NULL, CGRectGetMidX(frame), CGRectGetMidY(frame) );
    for (int index = 0; index < numberOfShakes; ++index)
    {
        CGPathAddLineToPoint(shakePath, NULL, CGRectGetMidX(frame) - frame.size.width * vigourOfShake,CGRectGetMidY(frame));
        CGPathAddLineToPoint(shakePath, NULL,  CGRectGetMidX(frame) + frame.size.width * vigourOfShake,CGRectGetMidY(frame));
    }
    CGPathCloseSubpath(shakePath);
    shakeAnimation.path = shakePath;
    shakeAnimation.duration = durationOfShake;
    CFRelease(shakePath);
    return shakeAnimation;
}
@end
